{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Size of the Dataset: (287136, 15)\n"
     ]
    },
    {
     "data": {
      "text/plain": "                                     lat         lon  StarkeyTime    GMDate  \\\ntraj_id   DateTime                                                            \n880109D01 1995-04-13 13:40:06  45.239682 -118.533204    229902006  21:40:06   \n          1995-04-15 12:16:15  45.250521 -118.530438    230069775  20:16:15   \n          1995-04-15 21:39:38  45.247943 -118.541455    230103578  05:39:38   \n          1995-04-16 03:32:14  45.247429 -118.539530    230124734  11:32:14   \n          1995-04-16 04:08:28  45.247117 -118.542579    230126908  12:08:28   \n\n                                 GMTime   LocDate   LocTime  RadNum Species  \\\ntraj_id   DateTime                                                            \n880109D01 1995-04-13 13:40:06  19950413  19950413  13:40:06     409       D   \n          1995-04-15 12:16:15  19950415  19950415  12:16:15     409       D   \n          1995-04-15 21:39:38  19950416  19950415  21:39:38     409       D   \n          1995-04-16 03:32:14  19950416  19950416  03:32:14     409       D   \n          1995-04-16 04:08:28  19950416  19950416  04:08:28     409       D   \n\n                                 UTME     UTMN  Year  Grensunr  Grensuns  \\\ntraj_id   DateTime                                                         \n880109D01 1995-04-13 13:40:06  379662  5010734    95  13:13:00  02:39:00   \n          1995-04-15 12:16:15  379895  5011927    95  13:09:00  02:41:00   \n          1995-04-15 21:39:38  379039  5011656    95  13:07:00  02:43:00   \n          1995-04-16 03:32:14  379188  5011581    95  13:07:00  02:43:00   \n          1995-04-16 04:08:28  378938  5011567    95  13:07:00  02:43:00   \n\n                               Obswt  \ntraj_id   DateTime                    \n880109D01 1995-04-13 13:40:06   1.47  \n          1995-04-15 12:16:15   1.59  \n          1995-04-15 21:39:38   1.34  \n          1995-04-16 03:32:14   1.50  \n          1995-04-16 04:08:28   1.34  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>lat</th>\n      <th>lon</th>\n      <th>StarkeyTime</th>\n      <th>GMDate</th>\n      <th>GMTime</th>\n      <th>LocDate</th>\n      <th>LocTime</th>\n      <th>RadNum</th>\n      <th>Species</th>\n      <th>UTME</th>\n      <th>UTMN</th>\n      <th>Year</th>\n      <th>Grensunr</th>\n      <th>Grensuns</th>\n      <th>Obswt</th>\n    </tr>\n    <tr>\n      <th>traj_id</th>\n      <th>DateTime</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">880109D01</th>\n      <th>1995-04-13 13:40:06</th>\n      <td>45.239682</td>\n      <td>-118.533204</td>\n      <td>229902006</td>\n      <td>21:40:06</td>\n      <td>19950413</td>\n      <td>19950413</td>\n      <td>13:40:06</td>\n      <td>409</td>\n      <td>D</td>\n      <td>379662</td>\n      <td>5010734</td>\n      <td>95</td>\n      <td>13:13:00</td>\n      <td>02:39:00</td>\n      <td>1.47</td>\n    </tr>\n    <tr>\n      <th>1995-04-15 12:16:15</th>\n      <td>45.250521</td>\n      <td>-118.530438</td>\n      <td>230069775</td>\n      <td>20:16:15</td>\n      <td>19950415</td>\n      <td>19950415</td>\n      <td>12:16:15</td>\n      <td>409</td>\n      <td>D</td>\n      <td>379895</td>\n      <td>5011927</td>\n      <td>95</td>\n      <td>13:09:00</td>\n      <td>02:41:00</td>\n      <td>1.59</td>\n    </tr>\n    <tr>\n      <th>1995-04-15 21:39:38</th>\n      <td>45.247943</td>\n      <td>-118.541455</td>\n      <td>230103578</td>\n      <td>05:39:38</td>\n      <td>19950416</td>\n      <td>19950415</td>\n      <td>21:39:38</td>\n      <td>409</td>\n      <td>D</td>\n      <td>379039</td>\n      <td>5011656</td>\n      <td>95</td>\n      <td>13:07:00</td>\n      <td>02:43:00</td>\n      <td>1.34</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 03:32:14</th>\n      <td>45.247429</td>\n      <td>-118.539530</td>\n      <td>230124734</td>\n      <td>11:32:14</td>\n      <td>19950416</td>\n      <td>19950416</td>\n      <td>03:32:14</td>\n      <td>409</td>\n      <td>D</td>\n      <td>379188</td>\n      <td>5011581</td>\n      <td>95</td>\n      <td>13:07:00</td>\n      <td>02:43:00</td>\n      <td>1.50</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 04:08:28</th>\n      <td>45.247117</td>\n      <td>-118.542579</td>\n      <td>230126908</td>\n      <td>12:08:28</td>\n      <td>19950416</td>\n      <td>19950416</td>\n      <td>04:08:28</td>\n      <td>409</td>\n      <td>D</td>\n      <td>378938</td>\n      <td>5011567</td>\n      <td>95</td>\n      <td>13:07:00</td>\n      <td>02:43:00</td>\n      <td>1.34</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from ptrail.core.TrajectoryDF import PTRAILDataFrame\n",
    "\n",
    "\"\"\"\n",
    "    First, to begin with the following actions ar performed:\n",
    "        1. Read the database from the csv file using pandas.\n",
    "        2. Convert the pandas dataframe to PTRAILDataFrame DataFrame\n",
    "           in order to represent the data with PTRAIL.\n",
    "\n",
    "    It is to be duly noted that the Starkey Project database provided\n",
    "    originally has been modified here to meet with the needs of\n",
    "    PTRAIL Library.\n",
    "\"\"\"\n",
    "pdf = pd.read_csv('./data/starkey.csv')\n",
    "starkey = PTRAILDataFrame(data_set=pdf,\n",
    "                          latitude='lat',\n",
    "                          longitude='lon',\n",
    "                          datetime='DateTime',\n",
    "                          traj_id='Id')\n",
    "print(\"Size of the Dataset: {}\".format(starkey.shape))\n",
    "starkey.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "'\\nOur course of action\\n\\n1. First, generate a set of spatial and temporal features on the\\n   dataset.\\n2. Then, based on the above generated features, clean the dataset\\n   to reduce noise as well as make the trajectories smoother.\\n3. Then, once the noise has been cleared, we will further make\\n   the trajectory smoother, we will interpolate the trajectories.\\n   Now, an important point to consider for interpolation is that\\n   only 4 fundamental columns are returned.\\n4. Then, train various models based on the original starkey dataset.\\n5. Based on that trained model, we will predict the species of the\\n   animal for the interpolated and cleaned dataset.\\n'"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\"\"\"\n",
    "Our course of action\n",
    "\n",
    "1. First, generate a set of spatial and temporal features on the\n",
    "   dataset.\n",
    "2. Then, based on the above generated features, clean the dataset\n",
    "   to reduce noise as well as make the trajectories smoother.\n",
    "3. Then, once the noise has been cleared, we will further make\n",
    "   the trajectory smoother, we will interpolate the trajectories.\n",
    "   Now, an important point to consider for interpolation is that\n",
    "   only 4 fundamental columns are returned.\n",
    "4. Then, train various models based on the original starkey dataset.\n",
    "5. Based on that trained model, we will predict the species of the\n",
    "   animal for the interpolated and cleaned dataset.\n",
    "\"\"\""
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.35 s, sys: 212 ms, total: 1.56 s\n",
      "Wall time: 1.92 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Step - 1: Feature generation.\n",
    "# The following features are being generated:\n",
    "#   1. Distance between consecutive points of the trajectory.\n",
    "from ptrail.features.kinematic_features import KinematicFeatures\n",
    "from ptrail.features.temporal_features import TemporalFeatures\n",
    "\n",
    "starkey_feat = KinematicFeatures.create_distance_column(starkey)\n",
    "starkey_feat = TemporalFeatures.create_time_of_day_column(starkey_feat)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length of original Dataframe: 287136\n",
      "Length of DataFrame after filtering: 262207\n",
      "CPU times: user 920 ms, sys: 230 ms, total: 1.15 s\n",
      "Wall time: 28.7 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Step - 2: Filtering.\n",
    "\n",
    "# 2.1: Hampel filter based on distance between consecutive points.\n",
    "from ptrail.preprocessing.filters import Filters\n",
    "\n",
    "hamp_filt_starkey = Filters.hampel_outlier_detection(dataframe=starkey_feat,\n",
    "                                                     column_name='Distance')\n",
    "print(f\"Length of original Dataframe: {len(starkey_feat)}\")\n",
    "print(f\"Length of DataFrame after filtering: {len(hamp_filt_starkey)}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length of original Dataframe: 262207\n",
      "Length of DataFrame after filtering: 262206\n"
     ]
    }
   ],
   "source": [
    "# 2.2: Remove duplicate points from the trajectories.\n",
    "\n",
    "dupl_filt_starkey = Filters.remove_duplicates(dataframe=hamp_filt_starkey)\n",
    "dupl_filt_starkey = dupl_filt_starkey.drop_duplicates(subset=['DateTime', 'traj_id'])\n",
    "print(f\"Length of original Dataframe: {len(hamp_filt_starkey)}\")\n",
    "print(f\"Length of DataFrame after filtering: {len(dupl_filt_starkey)}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length of original Dataframe: 262206\n",
      "Length of DataFrame after filtering: 262206\n"
     ]
    }
   ],
   "source": [
    "# 2.3 Remove trajectories with few points.\n",
    "\n",
    "few_filt_starkey = Filters.remove_trajectories_with_less_points(dataframe=dupl_filt_starkey,\n",
    "                                                                num_min_points=5)\n",
    "print(f\"Length of original Dataframe: {len(dupl_filt_starkey)}\")\n",
    "print(f\"Length of DataFrame after filtering: {len(few_filt_starkey)}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "False    223302\nTrue      38904\nName: DateTime, dtype: int64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = few_filt_starkey.reset_index()['DateTime'].diff().dt.total_seconds()\n",
    "(a > 3600*4).value_counts()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1.6 s, sys: 203 ms, total: 1.8 s\n",
      "Wall time: 22.3 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Step -3: Interpolate Trajectory.\n",
    "# We will use cubic interpolation here.\n",
    "import datetime as dt\n",
    "\n",
    "def dtt2timestamp(dtt):\n",
    "    ts = (dtt.hour * 60 + dtt.minute) * 60 + dtt.second\n",
    "    #if you want microseconds as well\n",
    "    ts += dtt.microsecond * 10**(-6)\n",
    "    return ts\n",
    "\n",
    "from ptrail.preprocessing.interpolation import Interpolation\n",
    "\n",
    "ip_starkey = Interpolation.interpolate_position(dataframe=few_filt_starkey,\n",
    "                                                time_jump=3600*4,\n",
    "                                                ip_type='cubic')\n",
    "\n",
    "# Add Date and Time column and convert to ordinal values\n",
    "# to make them eligible for the model fitting.\n",
    "ip_starkey = TemporalFeatures.create_time_column(ip_starkey)\n",
    "ip_starkey = TemporalFeatures.create_date_column(ip_starkey)\n",
    "ip_starkey['Date'] = ip_starkey['Date'].map(dt.datetime.toordinal)\n",
    "ip_starkey['Time'] = ip_starkey['Time'].apply(dtt2timestamp)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "                                     lat         lon  StarkeyTime    GMDate  \\\ntraj_id   DateTime                                                            \n880109D01 1995-04-13 13:40:06  45.239682 -118.533204    229902006  21:40:06   \n          1995-04-16 03:32:14  45.247429 -118.539530    230124734  11:32:14   \n          1995-04-16 04:08:28  45.247117 -118.542579    230126908  12:08:28   \n          1995-04-16 05:30:40  45.246359 -118.538736    230131840  13:30:40   \n          1995-04-16 06:03:39  45.246655 -118.536833    230133819  14:03:39   \n\n                                 GMTime   LocDate   LocTime  RadNum  Species  \\\ntraj_id   DateTime                                                             \n880109D01 1995-04-13 13:40:06  19950413  19950413  13:40:06     409        0   \n          1995-04-16 03:32:14  19950416  19950416  03:32:14     409        0   \n          1995-04-16 04:08:28  19950416  19950416  04:08:28     409        0   \n          1995-04-16 05:30:40  19950416  19950416  05:30:40     409        0   \n          1995-04-16 06:03:39  19950416  19950416  06:03:39     409        0   \n\n                                 UTME     UTMN  Year  Grensunr  Grensuns  \\\ntraj_id   DateTime                                                         \n880109D01 1995-04-13 13:40:06  379662  5010734    95  13:13:00  02:39:00   \n          1995-04-16 03:32:14  379188  5011581    95  13:07:00  02:43:00   \n          1995-04-16 04:08:28  378938  5011567    95  13:07:00  02:43:00   \n          1995-04-16 05:30:40  379258  5011468    95  13:07:00  02:43:00   \n          1995-04-16 06:03:39  379404  5011489    95  13:07:00  02:43:00   \n\n                               Obswt    Distance    Time_Of_Day  \ntraj_id   DateTime                                               \n880109D01 1995-04-13 13:40:06   1.47         NaN           Noon  \n          1995-04-16 03:32:14   1.50  161.204428     Late Night  \n          1995-04-16 04:08:28   1.34  241.258531     Late Night  \n          1995-04-16 05:30:40   1.46  312.474615  Early Morning  \n          1995-04-16 06:03:39   1.65  152.598529  Early Morning  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>lat</th>\n      <th>lon</th>\n      <th>StarkeyTime</th>\n      <th>GMDate</th>\n      <th>GMTime</th>\n      <th>LocDate</th>\n      <th>LocTime</th>\n      <th>RadNum</th>\n      <th>Species</th>\n      <th>UTME</th>\n      <th>UTMN</th>\n      <th>Year</th>\n      <th>Grensunr</th>\n      <th>Grensuns</th>\n      <th>Obswt</th>\n      <th>Distance</th>\n      <th>Time_Of_Day</th>\n    </tr>\n    <tr>\n      <th>traj_id</th>\n      <th>DateTime</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">880109D01</th>\n      <th>1995-04-13 13:40:06</th>\n      <td>45.239682</td>\n      <td>-118.533204</td>\n      <td>229902006</td>\n      <td>21:40:06</td>\n      <td>19950413</td>\n      <td>19950413</td>\n      <td>13:40:06</td>\n      <td>409</td>\n      <td>0</td>\n      <td>379662</td>\n      <td>5010734</td>\n      <td>95</td>\n      <td>13:13:00</td>\n      <td>02:39:00</td>\n      <td>1.47</td>\n      <td>NaN</td>\n      <td>Noon</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 03:32:14</th>\n      <td>45.247429</td>\n      <td>-118.539530</td>\n      <td>230124734</td>\n      <td>11:32:14</td>\n      <td>19950416</td>\n      <td>19950416</td>\n      <td>03:32:14</td>\n      <td>409</td>\n      <td>0</td>\n      <td>379188</td>\n      <td>5011581</td>\n      <td>95</td>\n      <td>13:07:00</td>\n      <td>02:43:00</td>\n      <td>1.50</td>\n      <td>161.204428</td>\n      <td>Late Night</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 04:08:28</th>\n      <td>45.247117</td>\n      <td>-118.542579</td>\n      <td>230126908</td>\n      <td>12:08:28</td>\n      <td>19950416</td>\n      <td>19950416</td>\n      <td>04:08:28</td>\n      <td>409</td>\n      <td>0</td>\n      <td>378938</td>\n      <td>5011567</td>\n      <td>95</td>\n      <td>13:07:00</td>\n      <td>02:43:00</td>\n      <td>1.34</td>\n      <td>241.258531</td>\n      <td>Late Night</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 05:30:40</th>\n      <td>45.246359</td>\n      <td>-118.538736</td>\n      <td>230131840</td>\n      <td>13:30:40</td>\n      <td>19950416</td>\n      <td>19950416</td>\n      <td>05:30:40</td>\n      <td>409</td>\n      <td>0</td>\n      <td>379258</td>\n      <td>5011468</td>\n      <td>95</td>\n      <td>13:07:00</td>\n      <td>02:43:00</td>\n      <td>1.46</td>\n      <td>312.474615</td>\n      <td>Early Morning</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 06:03:39</th>\n      <td>45.246655</td>\n      <td>-118.536833</td>\n      <td>230133819</td>\n      <td>14:03:39</td>\n      <td>19950416</td>\n      <td>19950416</td>\n      <td>06:03:39</td>\n      <td>409</td>\n      <td>0</td>\n      <td>379404</td>\n      <td>5011489</td>\n      <td>95</td>\n      <td>13:07:00</td>\n      <td>02:43:00</td>\n      <td>1.65</td>\n      <td>152.598529</td>\n      <td>Early Morning</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Modifying the data of the Species column to indicate the\n",
    "# results as follows:\n",
    "#   D (Deer) : 0\n",
    "#   E (Elk): 1\n",
    "#   C (Cattle): 2\n",
    "\n",
    "# First, doing the above mentioned conversion for the test\n",
    "# dataset.\n",
    "int_test = []\n",
    "for i in range(len(few_filt_starkey['Species'])):\n",
    "    if few_filt_starkey['Species'].iloc[i] == 'D':\n",
    "        int_test.append(0)\n",
    "    elif few_filt_starkey['Species'].iloc[i] == 'E':\n",
    "        int_test.append(1)\n",
    "    else:\n",
    "        int_test.append(2)\n",
    "few_filt_starkey['Species'] = int_test\n",
    "few_filt_starkey = few_filt_starkey.drop(columns='index')\n",
    "few_filt_starkey.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "                                     lat         lon     Time    Date  Species\ntraj_id   DateTime                                                            \n880109D01 1995-04-13 13:40:06  45.239682 -118.533204  49206.0  728396        0\n          1995-04-13 17:40:06  45.163212 -117.320470  63606.0  728396        0\n          1995-04-16 03:32:14  45.247429 -118.539530  12734.0  728399        0\n          1995-04-16 04:08:28  45.247117 -118.542579  14908.0  728399        0\n          1995-04-16 05:30:40  45.246359 -118.538736  19840.0  728399        0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>lat</th>\n      <th>lon</th>\n      <th>Time</th>\n      <th>Date</th>\n      <th>Species</th>\n    </tr>\n    <tr>\n      <th>traj_id</th>\n      <th>DateTime</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">880109D01</th>\n      <th>1995-04-13 13:40:06</th>\n      <td>45.239682</td>\n      <td>-118.533204</td>\n      <td>49206.0</td>\n      <td>728396</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1995-04-13 17:40:06</th>\n      <td>45.163212</td>\n      <td>-117.320470</td>\n      <td>63606.0</td>\n      <td>728396</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 03:32:14</th>\n      <td>45.247429</td>\n      <td>-118.539530</td>\n      <td>12734.0</td>\n      <td>728399</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 04:08:28</th>\n      <td>45.247117</td>\n      <td>-118.542579</td>\n      <td>14908.0</td>\n      <td>728399</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1995-04-16 05:30:40</th>\n      <td>45.246359</td>\n      <td>-118.538736</td>\n      <td>19840.0</td>\n      <td>728399</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Now, fixing the Species column for the interpolated starkey dataset.\n",
    "\n",
    "# Create a list of all unique ids.\n",
    "ids_ = list(ip_starkey.reset_index()['traj_id'].value_counts().keys())\n",
    "\n",
    "df_chunks = []\n",
    "# Create a small chunk for each ID, then for the same ID in the\n",
    "# original dataset, grab the species and then again assign that\n",
    "# to the interpolated ID.\n",
    "for i in range(len(ids_)):\n",
    "    small = ip_starkey.reset_index().loc[ip_starkey.reset_index()['traj_id'] == ids_[i]]\n",
    "    spec = few_filt_starkey.reset_index().loc[few_filt_starkey.reset_index()['traj_id'] == ids_[i], 'Species'].iloc[0]\n",
    "    small['Species'] = spec\n",
    "    df_chunks.append(small)\n",
    "\n",
    "# Now, convert the dataframe with interpolated species to\n",
    "# PTRAILDataFrame.\n",
    "ip_starkey = PTRAILDataFrame(data_set=pd.concat(df_chunks),\n",
    "                           latitude='lat',\n",
    "                           longitude='lon',\n",
    "                           traj_id='traj_id',\n",
    "                           datetime='DateTime')\n",
    "ip_starkey.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "# Step-5: Getting the test and train datasets ready.\n",
    "small_train = few_filt_starkey.reset_index()[['DateTime', 'traj_id', 'lat', 'lon', 'Species']]\n",
    "\n",
    "np_train_starkey = PTRAILDataFrame(data_set=small_train,\n",
    "                                datetime='DateTime',\n",
    "                                traj_id='traj_id',\n",
    "                                latitude='lat',\n",
    "                                longitude='lon',\n",
    "                                rest_of_columns=['Species'])\n",
    "np_train_starkey = TemporalFeatures.create_date_column(np_train_starkey)\n",
    "np_train_starkey = TemporalFeatures.create_time_column(np_train_starkey)\n",
    "\n",
    "\n",
    "\n",
    "np_train_starkey['Date'] = np_train_starkey['Date'].map(dt.datetime.toordinal)\n",
    "np_train_starkey['Time'] = np_train_starkey['Time'].apply(dtt2timestamp)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "# Splitting train into x and y.\n",
    "\n",
    "np_train_x = np_train_starkey.drop(columns=['Species'])\n",
    "np_train_y = np_train_starkey.reset_index()['Species']"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "# Splitting test into x and y.\n",
    "\n",
    "np_test_x = ip_starkey.drop(columns=['Species'])\n",
    "np_test_y = ip_starkey.reset_index()['Species']"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 44.5 s, sys: 87.5 ms, total: 44.6 s\n",
      "Wall time: 44.6 s\n"
     ]
    },
    {
     "data": {
      "text/plain": "RandomForestClassifier()"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Model 1: RandomForestClassifier.\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "rf_model = RandomForestClassifier()\n",
    "rf_model.fit(np_train_x, np_train_y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Target on train data: [0 0 0 ... 2 2 2]\n",
      "Target on test data: [0 1 1 ... 1 1 1]\n",
      "CPU times: user 4.57 s, sys: 8.01 ms, total: 4.58 s\n",
      "Wall time: 4.58 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Now, lets predict the Species on the train and test dataset.\n",
    "rf_predict_train = rf_model.predict(np_train_x)\n",
    "print(f\"Target on train data: {rf_predict_train}\")\n",
    "\n",
    "rf_predict_test = rf_model.predict(np_test_x)\n",
    "print(f\"Target on test data: {rf_predict_test}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Training accuracy of RF is: 100.0%\n",
      "The Testing accuracy of RF is: 58.62215564685681%\n"
     ]
    }
   ],
   "source": [
    "# Finally, lets check the accuracy of the RF module.\n",
    "rf_train_accuracy = accuracy_score(np_train_y, rf_predict_train)\n",
    "print(f\"The Training accuracy of RF is: {rf_train_accuracy*100}%\")\n",
    "\n",
    "rf_test_accuracy = accuracy_score(np_test_y, rf_predict_test)\n",
    "print(f\"The Testing accuracy of RF is: {rf_test_accuracy*100}%\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The depth of decision tree is: 40\n",
      "CPU times: user 1.22 s, sys: 1e+03 ns, total: 1.22 s\n",
      "Wall time: 1.22 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Model - 2: DecisionTreeClassifier model.\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "dtc_model = DecisionTreeClassifier()\n",
    "dtc_model.fit(np_train_x, np_train_y)\n",
    "\n",
    "print(f\"The depth of decision tree is: {dtc_model.get_depth()}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Target on train data: [0 0 0 ... 2 2 2]\n",
      "Target on the test data: [1 1 1 ... 1 1 1]\n",
      "CPU times: user 42.2 ms, sys: 0 ns, total: 42.2 ms\n",
      "Wall time: 40.1 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Now predicting the values of the training dataset.\n",
    "dtc_predict_train = dtc_model.predict(np_train_x)\n",
    "print(f\"Target on train data: {dtc_predict_train}\")\n",
    "\n",
    "# Now, predicting the values of the test dataset.\n",
    "dtc_predict_test = dtc_model.predict(np_test_x)\n",
    "print(f\"Target on the test data: {dtc_predict_test}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Training accuracy is: 100.0%\n",
      "The Testing accuracy is: 56.17097173831452%\n"
     ]
    }
   ],
   "source": [
    "# Finally, lets check the accuracy of the model on both sets.\n",
    "dtc_train_accuracy = accuracy_score(np_train_y, dtc_predict_train)\n",
    "print(f\"The Training accuracy is: {dtc_train_accuracy*100}%\")\n",
    "\n",
    "dtc_test_accuracy = accuracy_score(np_test_y, dtc_predict_test)\n",
    "print(f\"The Testing accuracy is: {dtc_test_accuracy*100}%\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 33.6 ms, sys: 19 µs, total: 33.7 ms\n",
      "Wall time: 32.8 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": "GaussianNB()"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "\n",
    "# Model - 3: Gaussian Naive Bayes.\n",
    "\n",
    "nb_model = GaussianNB()\n",
    "nb_model.fit(np_train_x, np_train_y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Target on train data: [1 1 1 ... 1 1 1]\n",
      "Target on test data: [2 2 2 ... 2 2 2]\n",
      "CPU times: user 54.8 ms, sys: 7.99 ms, total: 62.8 ms\n",
      "Wall time: 61.2 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Now, lets predict the Species on the train and test dataset.\n",
    "nb_predict_train = nb_model.predict(np_train_x)\n",
    "print(f\"Target on train data: {nb_predict_train}\")\n",
    "\n",
    "nb_predict_test = nb_model.predict(np_test_x)\n",
    "print(f\"Target on test data: {nb_predict_test}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Training accuracy of NB is: 57.88120790523481%\n",
      "The Testing accuracy of NB is: 17.244643432454605%\n"
     ]
    }
   ],
   "source": [
    "# Finally, lets check the accuracy of the Bayes model.\n",
    "nb_train_accuracy = accuracy_score(np_train_y, nb_predict_train)\n",
    "print(f\"The Training accuracy of NB is: {nb_train_accuracy*100}%\")\n",
    "\n",
    "nb_test_accuracy = accuracy_score(np_test_y, nb_predict_test)\n",
    "print(f\"The Testing accuracy of NB is: {nb_test_accuracy*100}%\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 359 ms, sys: 7.89 ms, total: 366 ms\n",
      "Wall time: 365 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": "KNeighborsClassifier(n_neighbors=75)"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Model - 4: K Nearest Neighbors\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "knn_model = KNeighborsClassifier(n_neighbors=75)\n",
    "knn_model.fit(np_train_x, np_train_y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Target on train data: [1 1 0 ... 1 1 1]\n",
      "Target on test data: [1 1 1 ... 1 1 1]\n",
      "CPU times: user 22.7 s, sys: 716 ms, total: 23.4 s\n",
      "Wall time: 23.4 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Now, lets predict the Species on the train and test dataset.\n",
    "knn_predict_train = knn_model.predict(np_train_x)\n",
    "print(f\"Target on train data: {knn_predict_train}\")\n",
    "\n",
    "knn_predict_test = knn_model.predict(np_test_x)\n",
    "print(f\"Target on test data: {knn_predict_test}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Training accuracy of KNN is: 58.66189179500088%\n",
      "The Testing accuracy of KNN is: 57.417488433268346%\n"
     ]
    }
   ],
   "source": [
    "# Finally, lets check the accuracy of the KNN module.\n",
    "knn_train_accuracy = accuracy_score(np_train_y, knn_predict_train)\n",
    "print(f\"The Training accuracy of KNN is: {knn_train_accuracy*100}%\")\n",
    "\n",
    "knn_test_accuracy = accuracy_score(np_test_y, knn_predict_test)\n",
    "print(f\"The Testing accuracy of KNN is: {knn_test_accuracy*100}%\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 9.18 s, sys: 233 ms, total: 9.42 s\n",
      "Wall time: 906 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": "KMeans(n_clusters=3)"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Model - 5: K Means Clustering\n",
    "from sklearn.cluster import KMeans\n",
    "\n",
    "km_model = KMeans(n_clusters=3)\n",
    "km_model.fit(np_train_x, np_train_y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Target on train data: [0 2 2 ... 0 0 0]\n",
      "Target on test data: [1 1 1 ... 1 1 1]\n",
      "CPU times: user 323 ms, sys: 12.3 ms, total: 335 ms\n",
      "Wall time: 29.8 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Now, lets predict the Species on the train and test dataset.\n",
    "km_predict_train = km_model.predict(np_train_x)\n",
    "print(f\"Target on train data: {km_predict_train}\")\n",
    "\n",
    "km_predict_test = km_model.predict(np_test_x)\n",
    "print(f\"Target on test data: {km_predict_test}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Training accuracy of KM is: 33.22120775268301%\n",
      "The Testing accuracy of KM is: 57.417488433268346%\n"
     ]
    }
   ],
   "source": [
    "# Finally, lets check the accuracy of the KM module.\n",
    "km_train_accuracy = accuracy_score(np_train_y, km_predict_train)\n",
    "print(f\"The Training accuracy of KM is: {km_train_accuracy*100}%\")\n",
    "\n",
    "km_test_accuracy = accuracy_score(np_test_y, km_predict_test)\n",
    "print(f\"The Testing accuracy of KM is: {km_test_accuracy*100}%\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "%%time\n",
    "\n",
    "# Now, lets try another classification model which is the\n",
    "# Support Vector Machine and check its accuracy.\n",
    "# from sklearn.svm import SVC\n",
    "#\n",
    "# svc_model = SVC()\n",
    "# svc_model.fit(np_train_x, np_train_y)\n",
    "#\n",
    "#"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# %%time\n",
    "#\n",
    "# # Now, lets predict the Species on the train and test dataset.\n",
    "# svc_predict_train = svc_model.predict(np_train_x)\n",
    "# print(f\"Target on train data: {svc_predict_train}\")\n",
    "#\n",
    "# svc_predict_test = svc_model.predict(np_test_x)\n",
    "# print(f\"Target on test data: {svc_predict_test}\")\n",
    "#\n",
    "#"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# # Finally, lets check the accuracy of the SVC module.\n",
    "# svc_train_accuracy = accuracy_score(np_train_y, svc_predict_train)\n",
    "# print(f\"The Training accuracy og SVC is: {svc_train_accuracy*100}%\")\n",
    "#\n",
    "# svc_test_accuracy = accuracy_score(np_test_y, svc_predict_test)\n",
    "# print(f\"The Training accuracy og SVC is: {svc_test_accuracy*100}%\")\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}